<?php namespace Common\Model;
use Hdphp\Model\Model;
//商品模型
class Commodity extends Model{
	//指定表名
    protected $table = "commodity";
	
	//自动验证
	 protected $validate = array( 
        array('cmname','required','商品名称不能为空',3,3),
        array('aboutarticles','required','商品描述不能为空',3,3),
        array('service','required','商品服务不能为空',3,3),
        array('picture','method','商品图册不能为空',3,3),
        
    );
	
	//添加
	public function store(){
//		p($_POST);exit;
		//如果自动验证失败
	    if(!$this->create()) return false;
//		执行添加
//      压入列表图片地址
        if(isset($_POST['thumb1'])){$this->data['columnplot']=$_POST['thumb1'];};
		//压入数据库时间
        $this->data['registerdate']=time();
		$this->data['catagory_cid']=Q('post.cid');
		$this->data['brand_bid']=Q('post.bid');
		$cmid=$this->add();
//		实例化商品详情表
		$productdetils = new \Common\Model\Productdetils;
//		p($_POST);die;
		$productdetils->create();
		$productdetils->data['commodity_cmid']=$cmid;
		$productdetils->data['picture']=implode('|', Q('post.thumb2',array()));
//		判断图片是否存在
//		if(isset($_POST['thumb2'])){
//			$img=Q('post.thumb2');
//		    $picture=implode('|', $img);
////			压入数据库
//			$data = array(
//				'picture'=>$picture,
//				'aboutarticles'=>Q('post.aboutarticles'),
//				'service'=>Q('post.service'),
//				'commodity_cmid'=>$cmid
//			);
//       $productdetils->add($data);
//		}
		$productdetils->add();
//		实例化商品属性表
		$specs = new \Common\Model\Specs;
		//传过来的属性　
		$attr = Q('post.attr',array());
		//p($attr);
//		环属性表
        foreach ($attr as $k => $v) {
        	//p($v);
			//die();
			if(!empty($v)){
				$data = array(
				'sability' => $v,
				'typeattribute_taid' => $k,
				'commodity_cmid' => $cmid
				);
				$specs->add($data);
			}	
		}
		//传过来的规格　
		$spec = Q('post.spec',array());
//		循环规格表
        foreach ($spec as $k => $i) {
			foreach($i['value'] as $a=> $v){
				if(!empty($v)){
					$data = array(
					'loadedprice' => $i['price'][$a],
					'sability'=>$v,
					'typeattribute_taid' => $k,
					'commodity_cmid' => $cmid
					);
					$specs->add($data);
				}
			}
		}
		return true;
	}
//	商品修改
    public function edit(){
    	$cmid=Q('post.cmid',0,'intval');
    	if(!$this->create()) return false;
		//      压入列表图片地址
        if(!isset($_POST['columnplot'])){
        	$this->data['columnplot']='';
        }else{
            $this->data['columnplot']=$_POST['columnplot'];
        }
		$this->data['catagory_cid']=Q('post.cid');
		$this->data['brand_bid']=Q('post.bid');
		$this->save();
//2修改商品详细表
		$productdetils=new \Common\Model\Productdetils;
		$productdetils->data['commodity_cmid']=$cmid;
		$productdetils->create();
		//把数组变为字符串，把多张图片压入到数据库
		if(!isset($_POST['picture'])){
        	$productdetils->data['picture']='';
        }else{
            $productdetils->data['picture']=implode('|', Q('post.picture',array()));
        }
//		p($productdetils->data);die;
     	$productdetils->where("commodity_cmid=$cmid")->save();
		//3修改表article_tag
		//先删除
		$specs=new \Common\Model\Specs;
		$specs->where("commodity_cmid=$cmid")->delete();
		//再添加
//		p($_POST);exit;
		//规格添加
		$specs->create();
		foreach (Q('post.spec',array()) as $k => $v){
			foreach ($v['value'] as $kk => $vv) {
				if(!empty($vv)){
					$add = array(
						'sability' => $vv,
						'loadedprice'   => $v['price'][$kk],
						'typeattribute_taid' => $k,
						'commodity_cmid' => $cmid
					);
					$specs->add($add);	
				}			
			}
		}
		//属性添加
		foreach (Q('post.attr',array()) as $k=>$v){
			if(!empty($v)){
				$data=array(
				'sability' => $v,
				'typeattribute_taid' => $k,
				'commodity_cmid' => $cmid
				);
				$specs->add($data);	
			}		
		}
		return true;
	}




	
}